package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyWmhDto;
import cn.stylefeng.guns.sys.modular.system.service.BtyWmhService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;

/*
 * 监听器批处理数据
 * */
public class WmhExcelListener extends AnalysisEventListener<BtyWmhDto> {

    private BtyWmhService wmhService;

    public WmhExcelListener(){

    }

    public WmhExcelListener(BtyWmhService wmhService){
        this.wmhService = wmhService;
    }


    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 80;
    List<BtyWmhDto> list = new ArrayList<>();

    /**
     *
     * @param basic
     * @param analysisContext
     */
    @Override
    public void invoke(BtyWmhDto basic, AnalysisContext analysisContext) {

        basic.setId(UUIDUtils.getId());
        if(ToolUtil.isNotEmpty(basic.getCrkrq())){
            list.add(basic);
        }
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }
    private void saveData(){
        //调用saveData()方法
        wmhService.insertWmh(list);
    }
}